package org.example.algorithm.dp;

import java.util.Arrays;

public class UniquePathsWithObstaclesSolution {

    //0 1
    //0 0
    public static void main(String[] args) {
        int[][] obstacleGrid = {{0,1},{0,0}};
        UniquePathsWithObstaclesSolution solution = new UniquePathsWithObstaclesSolution();
        int res = solution.uniquePathsWithObstacles3(obstacleGrid);
        System.out.println(res);
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int row = obstacleGrid.length;
        int col = obstacleGrid[0].length;
        int[][] dp = new int[row][col];
        dp[0][0] = obstacleGrid[0][0] != 1 ? 1 : 0;
        for (int i=1;i<row;i++) {
            if (obstacleGrid[i][0] != 1) {
                dp[i][0] = dp[i-1][0];
            } else {
                dp[i][0] = 0;
            }
        }
        for (int j=1;j<col;j++) {
            if (obstacleGrid[0][j] != 1) {
                dp[0][j] = dp[0][j-1];
            } else {
                dp[0][j] = 0;
            }
        }
        for (int i=1;i<row;i++) {
            for (int j=1;j<col;j++) {
                if (obstacleGrid[i][j] == 1) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = dp[i][j-1] + dp[i-1][j];
                }
            }
        }
        return dp[row-1][col-1];
    }


    //简化处理
    public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
        int row = obstacleGrid.length;
        int col = obstacleGrid[0].length;
        int[][] dp = new int[row+1][col+1];
        //!!! 重要
        dp[0][1] = 1;
        for (int i=0;i<row;i++) {
            for (int j=0;j<col;j++) {
                if (obstacleGrid[i][j] == 1) {
                    dp[i+1][j+1] = 0;
                } else {
                    dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j];
                }
            }
        }
        return dp[row][col];
    }

    //dfs+memory
    public int uniquePathsWithObstacles3(int[][] obstacleGrid) {
        int row = obstacleGrid.length;
        int col = obstacleGrid[0].length;
        int[][] mem = new int[row][col];
        for (int i=0;i<row;i++) {
            Arrays.fill(mem[i], -1);
        }
        return dfs(obstacleGrid, row-1, col-1, mem);
    }

    public int dfs(int[][] obstacleGrid, int i, int j, int[][] mem) {
        if (i<0 || j<0) {
            return 0;
        }
        if (mem[i][j] != -1) {
            return mem[i][j];
        }
        if (i == 0 && j == 0) {
            return mem[i][j] = obstacleGrid[i][j] == 1 ? 0 : 1;
        }
        if (i == 0) {
            if (obstacleGrid[i][j] == 1) {
                return mem[i][j] = 0;
            } else {
                return mem[i][j] = dfs(obstacleGrid, i, j-1, mem);
            }
        }
        if (j == 0) {
            if (obstacleGrid[i][j] == 1) {
                return mem[i][j] = 0;
            } else {
                return mem[i][j] = dfs(obstacleGrid, i-1, j, mem);
            }
        }
        return mem[i][j] =  obstacleGrid[i][j] == 1 ? 0 : (dfs(obstacleGrid, i, j-1, mem) + dfs(obstacleGrid, i-1, j, mem));
    }
}
